<?php

namespace app\admin\controller;
use PHPMailer\SendEmail;

/**
 *
 * 系统设置，SEO
 * Class System
 * @package app\admin\controller
 */
class System extends base
{

    /**
     * 列表页
     */
    public function index()
    {
        $type = $this->request->param('type');
        $data = cache('system'.$type.cookie('think_var_admin'));

        if(empty($data)){
            $data= $this->model->getadminnoteall($type);
            cache('system'.$type.cookie('think_var_admin'),$data);
        }

        $this->assign('data', $data);
        $this->assign('type', $type);


        $this->assign('pctemplate', templateList('pc'));
        $this->assign('mobiletemplate', templateList('mobile'));

        return $this->fetch($type . '/index');
    }


    /**
     * 保存
     */
    public function save()
    {
        if (!$this->request->isPost()) {
            return ajax_info('请求失败');
        }

        $data = $this->request->post();

        $cheak = [];

        $cheak = $this->model->getadminnoteall($data['type']);
        foreach ($data as $key => $value) {
            if ($key != 'type' && $cheak[$key]['value'] != $value) {
                if (file_exists(__IMAGE__. $cheak[$key]['value']) && $cheak[$key]['value']) {
                    unlink(__IMAGE__. $cheak[$key]['value']);
                }
                $this->model->save(['value' => html_entity_decode($value)], ['title' => $key, 'type' => $data['type'],'lang'=>cookie('think_var_admin')]);
            }
        }
        cache('system'.$data['type'].cookie('think_var_admin'),null);
        cache('wechat'.cookie('think_var_admin'),null);
        return ajax_info('修改成功', 'index');
    }

    /**
     * 发送测试邮箱
     */
    public function checkmail(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $dd = new SendEmail();
            $result= $dd->SendEmail($data['to'], $data['title'], html_entity_decode($data['content']));

            if($result=='1'){
                return ajax_info('邮件发送成功！');
            }else{
                return ajax_info('邮箱发送失败，请联系管理员！'.$result);
            }
        }
        $data = cache('system'.$this->request->controller().cookie('think_var_admin'));
        if(empty($data)){
            $data= $this->model->getnoteall($this->request->controller());
            cache('system'.$this->request->controller().cookie('think_var_admin'),$data);
        }
        $this->assign('data', $data);

        return $this->fetch();
    }
}
